<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="">
      <el-form-item label="" prop="node">
        <el-input
          v-model="queryParams.node"
          placeholder="请输入当前节点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="toNode">
        <el-input
          v-model="queryParams.toNode"
          placeholder="请输入指向节点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="flowKey">
        <el-select v-model="queryParams.flowKey" placeholder="请选择" style="width: 100%;" clearable>
          <el-option v-for="item in workArr" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activiti:dispose:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activiti:dispose:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activiti:dispose:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activiti:dispose:export']"
        >导出</el-button>
      </el-col> -->
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>
    <!-- @selection-change="handleSelectionChange" -->
    <el-table v-loading="loading" :data="disposeList" :max-height="height" :height="height">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="处理名称" align="center" prop="name" min-width="120" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="值" align="center" prop="code" />
      <el-table-column label="当前id值" align="center" prop="takeId" />
      <el-table-column label="创建时间" align="center" prop="createTimeN" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTimeN, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTimeN" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTimeN, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="当前节点名称" align="center" prop="node" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="指向节点名称" align="center" prop="toNode" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="流程唯一标识key" align="center" prop="flowKey" min-width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.flowKey == 'batch'">呈批件</span>
          <span v-else-if="scope.row.flowKey == 'dispatch'">发文</span>
          <span v-else-if="scope.row.flowKey == 'message'">收文</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column label="当前角色" align="center" prop="role" />
      <el-table-column label="下节点角色" align="center" prop="toRole" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" min-width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activiti:dispose:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activiti:dispose:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作流配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="处理名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入处理名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="值" prop="code">
              <el-input v-model="form.code" placeholder="请输入值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="当前id值" prop="takeId">
              <el-input v-model="form.takeId" placeholder="请输入当前id值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前节点名称" prop="node">
              <el-input v-model="form.node" placeholder="请输入当前节点名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="指向节点名称" prop="toNode">
              <el-input v-model="form.toNode" placeholder="请输入指向节点名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流程唯一标识key" prop="flowKey">
              <el-select v-model="form.flowKey" placeholder="请选择" style="width: 100%;" clearable>
                <el-option v-for="item in workArr" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="当前角色" prop="role">
              <el-input v-model="form.role" placeholder="请输入当前角色" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下节点角色" prop="toRole">
              <el-input v-model="form.toRole" placeholder="请输入下节点角色" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDispose, getDispose, delDispose, addDispose, updateDispose, exportDispose } from "@/api/activiti/dispose";
import { getWorkList } from "@/utils/ruoyi"

export default {
  name: "Dispose",
  data() {
    return {
      height: (document.documentElement.clientHeight || document.body.clientHeight)-260,
      // 流程标识KEY列表
      workArr: getWorkList(),
      // 遮罩层
      loading: true,
      submitLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作流配置表格数据
      disposeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        node: null,
        toNode: null,
        flowKey: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required:true, message:'请输入处理名称', trigger:'blur' }],
        code: [{ required:true, message:'请输入值', trigger:'blur' }],
        takeId: [{ required:true, message:'请输入当前id值', trigger:'blur' }],
        node: [{ required:true, message:'请输入当前节点名称', trigger:'blur' }],
        toNode: [{ required:true, message:'请输入指向节点名称', trigger:'blur' }],
        flowKey: [{ required:true, message:'请选择流程唯一标识key', trigger:'change' }],
        role: [{ required:true, message:'请输入当前角色', trigger:'blur' }],
        toRole: [{ required:true, message:'请输入下节点角色', trigger:'blur' }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工作流配置列表 */
    getList() {
      this.loading = true;
      listDispose(this.queryParams).then(response => {
        this.disposeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        takeId: null,
        // createTimeN: null,
        // updateTimeN: null,
        node: null,
        toNode: null,
        flowKey: null,
        role: null,
        toRole: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作流配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDispose(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作流配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id != null) {
            updateDispose(this.form).then(response => {
              this.submitLoading = false
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDispose(this.form).then(response => {
              this.submitLoading = false
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除处理名称为"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDispose(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工作流配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDispose(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
